/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.service.bmp.biz.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.xbdun.common.exceptions.BizException;
import com.xbdun.common.page.Pager;
import com.xbdun.dao.bmp.entity.XbdunPlatformInfoDo;
import com.xbdun.dao.bmp.entity.XbdunSysMenuDo;
import com.xbdun.dao.bmp.mapper.PlatformInfoMapper;
import com.xbdun.dao.bmp.mapper.SysMenuMapper;
import com.xbdun.facade.bmp.enumx.SysMenuEnum.SysMenuEnableEnum;
import com.xbdun.facade.bmp.enumx.SysMenuEnum.SysMenuTargetEnum;
import com.xbdun.facade.bmp.exceptions.BmpBizException;
import com.xbdun.service.bmp.biz.SysMenuBiz;


/**
 * 菜单表
 * @ClassName XbdunSysMenuServiceImpl
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
@Service
@Transactional
public class SysMenuBizImpl implements SysMenuBiz {
	@Autowired
	private SysMenuMapper sysMenuMapper;
	@Autowired
	private PlatformInfoMapper platformInfoMapper;
	
	Log log = LogFactory.getLog(SysMenuBizImpl.class);

	/**
	 * 根据主键获取对象
	 * @return {@link XbdunSysMenuDo}
	 */
	public XbdunSysMenuDo getXbdunSysMenuById(long id)throws RuntimeException{
		Assert.isTrue(id>0, "主键id不能小于零");
		
		return sysMenuMapper.getXbdunSysMenuById(id);
	}
	
	/**
	 * 新增
	 * @param xbdunSysMenuDo 
	 * @param platCode 平台编号
	 */
	public Long addXbdunSysMenu(XbdunSysMenuDo xbdunSysMenuDo,Integer platCode)throws RuntimeException{
		Assert.notNull(xbdunSysMenuDo,"新增xbdunSysMenuDo对象不能为Null");
		if(null!=platCode && platCode.intValue()>0){
			XbdunPlatformInfoDo byPlatCode = platformInfoMapper.getByPlatCode(platCode);
			if(null == byPlatCode){
				throw BmpBizException.PLAT_NOT_EXIST.newInstance("新增菜单平台可能未启用,平台编号[%s]",platCode);
			}
			Map<String,Object> param = new HashMap<String,Object>();
			
			param.put("code", platCode.intValue());
			if(sysMenuMapper.existXbdunSysMenu(param)>0){
				param.clear();
				throw BmpBizException.DATA_EXIST.newInstance("应用编号【"+platCode+"】菜单已经存在");
			}
			xbdunSysMenuDo.setMenuType(1);//默认目录
			xbdunSysMenuDo.setName(byPlatCode.getPlatName());
			xbdunSysMenuDo.setHit(byPlatCode.getPlatName());
			xbdunSysMenuDo.setUrl(StringUtils.EMPTY);
			xbdunSysMenuDo.setBlPlat(1);
			//如果平台Code存在，那么菜单Code为空
			xbdunSysMenuDo.setCode(0);
		}
		sysMenuMapper.addXbdunSysMenu(xbdunSysMenuDo,platCode);
		long code = sysMenuMapper.selectToCode(xbdunSysMenuDo.getId());
		if(0==sysMenuMapper.updateMenuSort(xbdunSysMenuDo.getId(),code)){
			throw BizException.DB_INSERT_RESULT_0;
		}
		return xbdunSysMenuDo.getId();
	}
	/**
	 * 删除数据根据主键
	 * @param idx
	 */
	public void delXbdunSysMenuById(long id)throws RuntimeException{
		Assert.isTrue(id>0, "主键id不能小于零");
		
		sysMenuMapper.delXbdunSysMenuById(id);
	}
	
	/**
	 * 判断是否存在,不为null则是存在
	 * @param id 主键id
	 * @return Long
	 */
	public boolean existById(long id)throws RuntimeException{
		Assert.isTrue(id>0,"参数id必须大于零");
		Map<String,Object> param = new HashMap<String, Object>();
		
		//根据主键
		param.put("id",id);
		
		int rlt = sysMenuMapper.existXbdunSysMenu(param);

		param.clear();
		param = null;

		return (rlt>0);
	}
	
	/**
	 * 更新对象属性
	 * @param XbdunSysMenuDo	更新对象
	 * @param id
	 */
	public void updateById(XbdunSysMenuDo xbdunSysMenuDo,long id)throws RuntimeException{
		
		Assert.isTrue(null!=xbdunSysMenuDo,"更新xbdunSysMenuDo对象不能为空");
		
		Map<String,Object> param = new HashMap<String,Object>();
		
		if (!StringUtils.isBlank(xbdunSysMenuDo.getName())) {// 修改菜单名称
			param.put("name", xbdunSysMenuDo.getName());
		}
		if (!StringUtils.isBlank(xbdunSysMenuDo.getHit())) {// 修改菜单title
			param.put("hit", xbdunSysMenuDo.getHit());
		}
		if (!StringUtils.isBlank(xbdunSysMenuDo.getUrl())) {// 修改菜单url
			param.put("url", xbdunSysMenuDo.getUrl());
		}
		if (!StringUtils.isBlank(xbdunSysMenuDo.getIcoUrl())) {// 修改菜单小图标
			param.put("icoUrl", xbdunSysMenuDo.getIcoUrl());
		}
		if (!StringUtils.isBlank(xbdunSysMenuDo.getTarget())) {// 修改菜单打开方式
			if (SysMenuTargetEnum.isValue(xbdunSysMenuDo.getTarget())) {
				param.put("hit", xbdunSysMenuDo.getHit());
			}
		}
		if (xbdunSysMenuDo.getSort() > 0) {// 设置菜单排序
			param.put("sort", xbdunSysMenuDo.getSort());
		}
		if (xbdunSysMenuDo.getEnable() != 0){// 设置菜单的启用状态
			if (SysMenuEnableEnum.isKey(xbdunSysMenuDo.getEnable())) {
				param.put("enable", xbdunSysMenuDo.getEnable());
			}
		}
		if (xbdunSysMenuDo.getBlDel() != 0){// 设置菜单的删除状态
			if (SysMenuEnableEnum.isKey(xbdunSysMenuDo.getBlDel())) {
				param.put("enable", xbdunSysMenuDo.getEnable());
			}
		}
		
		Map<String,Object> where = new HashMap<String,Object>();
		// 获取菜单的版本号
		Timestamp version = sysMenuMapper.getMenuVersion(id);
		
		where.put("id", id);
		where.put("version", version);
		
		//菜单更新失败
		if(0==sysMenuMapper.updateDynXbdunSysMenu(param, where)){
			param.clear();
			where.clear();
			throw BizException.DB_UPDATE_RESULT_0;
		}
		param.clear();
		where.clear();
		param = null;
		where = null;
	}
	
	/**
	 * 查询分页
	 * @param where			约束条件,键是对象属性其中条件都为and连接
	 * @param orderBy		排序条件
	 * @param pageNum   	当前索引页
	 * @param numPerPage	每页显示条数
	 * @return
	 * @throws RuntimeException
	 */
	public Pager<XbdunSysMenuDo> queryPager(Map<String,Object> where,String orderBy,int pageIndex,int pageSize){
		log.info(where);
		//分页对象
		Pager<XbdunSysMenuDo> resultPager = new Pager<XbdunSysMenuDo>();
		
		resultPager.setPageSize(pageSize);
		resultPager.setPageIndex(pageIndex);
		resultPager.setSort(orderBy);
		Long resultCnt = sysMenuMapper.queryPagerCount(where);
		if(null!=resultCnt&&resultCnt.longValue()==0){
			
			resultPager.setTotalCount(0);
			return resultPager;
		}
		
		//获取查询结果集List
		List<XbdunSysMenuDo> list = sysMenuMapper.queryPager(where, orderBy,(resultPager.getPageIndex()-1)*resultPager.getPageSize(), resultPager.getPageSize());
		
		//设置查询条数
		resultPager.setTotalCount(resultCnt.longValue());
		resultPager.setPager();
		resultPager.setData(list);
		
		return resultPager;
	}

	/**
	 * 获取菜单的子菜单的最大编码
	 * @param code			父级菜单编码
	 */
	public Long getAddMenuCode(long code) {
		Assert.isTrue(code >= 0 && code <= 999999999, "父级菜单编码超出范围;");
		return sysMenuMapper.getAddMenuCode(code);
	}
}